const app = getApp()
var $http = require('../../utils/api/login.js')
var common = require('../../utils/common.js')
var $ = require('../../utils/js.js');
var image_gy_src = $.image_gy_src;

Page({
  // 页面的初始数据
  data: {
    image_gy_src: image_gy_src,
    wxuserinfo: wx.getStorageSync('wxuserinfo'),
    mobile: '',
    captcha: '',
    yzm: '获取验证码',
    countdown: 60,
    disable: false,
    dashang_model: false
  },
  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    // 获取config
    this.get_config();

    // 清除token
    getApp().globalData.token = "";

    if (this.data.wxuserinfo) {
      this.setData({
        dashang_model: true
      })
    }
    this.modal = this.selectComponent("#modal");
  },
  // 获取config
  get_config() {
    $.ajax_get({
      url: "/common/xcx_config",
      success: (res) => {
        var data = res.data.data;
        this.setData({
          config: data,
        })
      }
    });
  },
  modal_click_Hidden() {
    this.setData({
      dashang_model: true
    })
  },
  bindGetUserInfo: function(e) {
    this.setData({
      wxuserinfo: e.detail.userInfo
    })
    wx.setStorageSync('wxuserinfo', e.detail.userInfo)

    this.modal_click_Hidden()
  },
  // 生命周期函数--监听页面显示
  onShow: function() {

  },
  // 自定义函数   开始
  // 微信登录
  wxlogin(e) {
    wx.getUserInfo({
      success: (res) => {
        wx.login({
          success: res => {
            $http.wx_login(res.code).then((res) => {
              var data = {
                iv: e.detail.iv,
                encryptedData: e.detail.encryptedData,
                sessionKey: res.data.session_key,
                openid: res.data.openid,
              }
              data = Object.assign(data, this.data.wxuserinfo)
              console.log("微信快捷登录", data);
              $http.wx_mobilelogin(data).then((res) => {
                if (res.data.code == 1) {
                  wx.showToast({
                    title: '登录成功',
                    icon: 'success',
                    duration: 2000
                  })
                  wx.setStorage({
                    key: "token",
                    data: res.data.data.userinfo.token,
                    success: function() {
                      app.globalData.token = res.data.data.userinfo.token
                      wx.setStorageSync('userinfo', res.data.data.userinfo)
                      app.globalData.userinfo = res.data.data.userinfo
                      console.log('sdsdsd:' + app.globalData.token)
                      wx.navigateBack();
                    }
                  })
                } else {
                  wx.showToast({
                    title: '登录失败',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            })
          }
        })
      },
      fail: () => {
        if (e.detail.errMsg == "getPhoneNumber:ok") {
          this.setData({
            dashang_model: false
          })
        }
      }
    })
  },
  // 手机号
  bindmobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // 获取验证码
  getcaptcha() {
    if (!common.isphone(this.data.mobile)) {
      return false
    }
    $http.send(this.data.mobile).then(res => {
      if (res.data.code == 1) {
        wx.showToast({
          title: '发送成功',
          icon: 'success',
          duration: 2000
        })
        this.settime()
      }
    })
  },
  //倒计时
  settime() {
    var that = this;
    if (that.data.countdown == 0) {
      that.setData({
        yzm: '获取验证码',
        countdown: 60,
        disable: false
      })
      return false;
    } else {
      that.setData({
        yzm: that.data.countdown + '秒',
        countdown: that.data.countdown - 1,
        disable: true
      })
    }
    setTimeout(function() {
      that.settime()
    }, 1000)
  },
  // 验证码
  bindcaptcha(e) {
    this.setData({
      captcha: e.detail.value
    })
  },
  // 立即注册
  login() {
    if (!common.isphone(this.data.mobile)) {
      return false
    }
    if (!this.data.captcha) {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.getUserInfo({
      success: (res) => {
        //验证
        $http.check(this.data.mobile, this.data.captcha).then((res) => {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          if (res.data.code == 1) {
            var data = {
              mobile: this.data.mobile,
              captcha: this.data.captcha,
              // avatarUrl: this.data.wxuserinfo.avatarUrl
            }
            data = Object.assign(data, this.data.wxuserinfo);
            console.log("手机号验证登录", data);
            $http.mobilelogin(data).then((res) => {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
              if (res.data.code == 1) {
                wx.setStorage({
                  key: "token",
                  data: res.data.data.userinfo.token,
                  success: function() {
                    app.globalData.token = res.data.data.userinfo.token
                    wx.setStorageSync('userinfo', res.data.data.userinfo)
                    app.globalData.userinfo = res.data.data.userinfo
                    wx.navigateBack();
                  }
                })
              }
            })
          }
        })
      },
      fail: () => {
        this.setData({
          dashang_model: false
        })
      }
    })
  },
  // 自定义函数   结束
  // 生命周期函数--监听页面隐藏
  onHide: function() {

  },
  // 生命周期函数--监听页面卸载
  onUnload: function() {

  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function() {

  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function() {

  },
  // 用户点击右上角分享
  // onShareAppMessage: function() {

  // }
})